import { apiClient } from './index'
import { mockApi } from '@/utils/mockApi'
import type { 
  AlertRule, 
  AlertHistory, 
  AlertTemplate,
  ApiResponse,
  PaginationResponse,
  QueryParams 
} from '@/types'

export const alertApi = {
  // 获取告警规则列表
  getRules: async (params?: QueryParams): Promise<ApiResponse<AlertRule[]>> => {
    // 模拟数据
    const mockRules: AlertRule[] = [
      {
        id: 1,
        name: '数据量异常检测',
        description: '检测表数据量异常变化',
        rule_type: 'data_volume',
        conditions: {
          threshold: 1000,
          change_rate: 0.2
        },
        severity: 'MEDIUM',
        is_active: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z'
      },
      {
        id: 2,
        name: '空表检测',
        description: '检测表是否为空',
        rule_type: 'custom',
        conditions: {
          empty_threshold: 0
        },
        severity: 'HIGH',
        is_active: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z'
      }
    ]
    
    return {
      code: 200,
      message: 'success',
      data: mockRules,
      timestamp: new Date().toISOString()
    }
  },

  // 获取告警历史
  getHistory: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<AlertHistory>>> => {
    return mockApi.alert.getHistory(params)
  },

  // 获取告警模板
  getTemplates: async (): Promise<ApiResponse<AlertTemplate[]>> => {
    return mockApi.alert.getTemplates()
  },

  // 创建告警规则
  createRule: async (data: Partial<AlertRule>): Promise<ApiResponse<AlertRule>> => {
    return {
      code: 200,
      message: 'success',
      data: data as AlertRule,
      timestamp: new Date().toISOString()
    }
  },

  // 更新告警规则
  updateRule: async (ruleId: number, data: Partial<AlertRule>): Promise<ApiResponse<AlertRule>> => {
    return {
      code: 200,
      message: 'success',
      data: data as AlertRule,
      timestamp: new Date().toISOString()
    }
  },

  // 删除告警规则
  deleteRule: async (ruleId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 启用/禁用告警规则
  toggleRule: async (ruleId: number, isActive: boolean): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 测试告警规则
  testRule: async (ruleId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  // 模板管理
  createTemplate: async (data: Partial<AlertTemplate>): Promise<ApiResponse<AlertTemplate>> => {
    return {
      code: 200,
      message: 'success',
      data: data as AlertTemplate,
      timestamp: new Date().toISOString()
    }
  },

  updateTemplate: async (templateId: number, data: Partial<AlertTemplate>): Promise<ApiResponse<AlertTemplate>> => {
    return {
      code: 200,
      message: 'success',
      data: data as AlertTemplate,
      timestamp: new Date().toISOString()
    }
  },

  deleteTemplate: async (templateId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  }
} 